name: Docker-compose test

on:
  pull_request:
  push:
    branches:
      - test 

jobs:
  tests:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository from specific branch
        uses: actions/checkout@v3
        with:
          ref: test 
      - name: find_docker_compose
        run: |
          result=$(find $GITHUB_WORKSPACE -name "docker-compose.yml")
          if [ -z "$result" ]; then
            echo "docker-compose.yml not found"
            exit 1  # Завершить выполнение
          else
            directory=$(dirname "$result")
            echo "Found docker-compose.yml at: $result"
          fi
          
      - name: Set up Docker
        uses: docker/setup-buildx-action@v3
      - name: Set up Docker Compose
        run: sudo apt-get update && sudo apt-get install docker-compose 
      - name: Build and Start Docker Compose
        run: docker compose up -d 
      - name: Check Services list
        run: docker-compose ps
      - name: Check api
        run: nc -z "127.0.0.1" "8001" && echo "api is available" || exit 1
      - name: Check postgres
        run: nc -z "127.0.0.1" "5432" && echo "postgres is available" || exit 1
      - name: Check front
        run: nc -z "127.0.0.1" "8080" && echo "front is available" || exit 1
      - name: Check redis
        run: nc -z "127.0.0.1" "6379" && echo "redis is available" || exit 1      
        
      - name: Shut Down Docker Compose
        if: always() 
        run: docker compose down
